package terra.model.object;

import terra.Config;
import terra.managers.MapManager;
import terra.model.World;
import terra.model.object.interfaces.Attackable;
import terra.model.object.interfaces.Attacker;
import terra.model.object.map.AbstractMapObject;
import terra.model.object.map.GroundObject;
import terra.model.object.map.MapObject;
import terra.network.packet.server.battle.Blow;

public class Dynamite extends ItemObject implements Attackable {
	private static final long serialVersionUID = 7239587294992286236L;
	private int radius = Config.tileSize * 5;
	private int power = 100;
	private int timer = 5000;
	public transient LiveObject owner;

	public Dynamite() {
		getKnownList().setAutoUpdate(250);
		getKnownList().knowDistance = radius;
		setPickupable(false);
	}

	@Override
	public void update(int delta) {
		if (isRemove())
			return;
		// if (needUpdate)
		super.update(delta);
		if (timer <= 0) {
			blow();
			return;
		}
		timer = timer - delta;
		needUpdate();
	}

	private void blow() {
		if (isRemove())
			return;
		setRemove(true);
		World.getInstance().removeObject(this);
		removeObject();
		sendBlow();
		for (GameObject object : getKnownList().getKnownObjects().values()) {
			if (object.isRemove() || object == this)
				continue;
			if (object instanceof Attackable)
				((Attackable) object).onHit((owner != null ? (Attacker) owner
						: null), power);
		}

		int minX = (int) (getX() / Config.tileSize - radius / Config.tileSize);
		int maxX = minX + (radius / Config.tileSize * 2 + 1);
		int minY = (int) (getY() / Config.tileSize - radius / Config.tileSize);
		int maxY = minY + (radius / Config.tileSize * 2 + 1);
		for (int x = minX; x < maxX; x++)
			for (int y = minY; y < maxY; y++) {
				AbstractMapObject obj = MapManager.instance.getTileFromTiles(x,
						y);
				if (obj == null)
					continue;
				if (obj.getType() != MapObject.TYPE_GROUND)
					continue;
				if (distanceTo(obj.getX() + Config.tileSize / 2, obj.getY()
						+ Config.tileSize / 2) < radius)
					((GroundObject) obj).hit(power);
			}
		for (GameObject object : getKnownList().getKnownObjects().values())
			object.needUpdate();
	}

	private void sendBlow() {
		Blow b = new Blow();
		b.x = (int) getX();
		b.y = (int) getY();
		b.r = 255;
		b.g = 69;
		b.b = 0;
		b.radius = radius / Config.tileSize + 3;
		b.time = 250;
		getMap().send(b, true);
		// broadcastPacket(b);
	}

	public int getPower() {
		return power;
	}

	public void setPower(int power) {
		this.power = power;
	}

	public int getRadius() {
		return radius;
	}

	public void setRadius(int radius) {
		this.radius = radius;
	}

	@Override
	public void onHit(Attacker damager, float damage) {
		// System.out.println("onHit blow");
		die(damager);
	}

	@Override
	public void die(Attacker killer) {
		// System.out.println("die blow");
		blow();
	}

	@Override
	public boolean isDead() {
		return isRemove();
	}

}
